﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pb;

public class HeadPortraitModel : MonoBehaviour
{

    private HallModel hallModel;
    public Transform GridPos;
    public UISprite UserIcon;
    public UITexture wxAvatar;
    public GameObject CloseBtn;
    public GameObject ConfirmBtn;//确认按钮
    public UILabel UserId;
    EginUser user = EginUser.Instance;
    void Awake()
    {
        hallModel = GameObject.FindObjectOfType<HallModel>().GetComponent<HallModel>();

        UserId.text = "ID:" + user.Uid ;
        UISprite Obj = hallModel.Icon.gameObject.GetComponent<UISprite>();
        UserIcon.spriteName = Obj.spriteName;
    }
    void Start()
    {

        if(user.loginControl == 1){           
            wxAvatar.mainTexture = hallModel.Avatar.mainTexture;
            wxAvatar.gameObject.SetActive(true);
        }
        
        for (int i = 0; i < 16; i++)
        {
            GridPos.GetChild(i).name = (i + 1).ToString();

            UIEventListener.Get(GridPos.GetChild(i).gameObject).onClick = ClickBtn;

        }
        UIEventListener.Get(ConfirmBtn).onClick = ClickCloseAndConfirmBtn;
        UIEventListener.Get(CloseBtn).onClick = ClickCloseAndConfirmBtn;

    }

    public void ClickBtn(GameObject Obj)
    {
        UserIcon.spriteName = Obj.name;
    }
    public void ClickCloseAndConfirmBtn(GameObject Obj)
    {
        if (Obj.name == "CloseBtn")
        {
            Destroy(gameObject);
        }
        else if (Obj.name == "ConfirmBtn")
        {
            int nuber;
            if (int.TryParse(UserIcon.spriteName,out nuber))
            {       
                ModifyAvatarRequest mo = new Pb.ModifyAvatarRequest { AvatarId = nuber};
                SocketManager socketManager = SocketManager.Instance;
                socketManager.SendProtobuf1(mo, MsgId.ModifyAvatarRequest);              
            }
        }

    }
  

}
